﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Adnc.Infra.Helper
{
    /// <summary>
    /// 信息脱敏
    /// </summary>
    public static class SensitiveHelper
    {
        /// <summary>
        /// 字符串脱敏
        /// </summary>
        /// <param name="data">字符串</param>
        /// <param name="replace">脱敏的代替字符</param>
        /// <param name="ReservedLen">头尾字符保留长度</param>
        /// <returns></returns>
        public static string GetSensitiveData(string data, char replace = '*',int ReservedLen = 1)
        {
            if (string.IsNullOrEmpty(data))
                return "";
            if (ReservedLen <= 0)
                return data;
            if (data.Length < ReservedLen)
                return data;
            else if (data.Length <= 2 * ReservedLen)
                return data.PadRight(data.Length - ReservedLen, replace);
            else
            {
                string head = data.Substring(0, ReservedLen);
                string tail = data.Substring(data.Length - ReservedLen, ReservedLen);
                return head.PadRight(data.Length - ReservedLen,replace) + tail;
            }
        }
    }
}
